#include <string>
#include <stdio.h>
#include <iostream>
#include <map>
#include <pthread.h>

#include "../headers/Mailer.h"

extern pthread_mutex_t msgCount_lock;

using namespace std;

int N = -7;
Mailer* mailer = NULL;


bool createNet(char* filename) {
	//parse the file
	cout << "createNet start "<< filename << endl;
	FILE *file;
	if ((file = fopen(filename, "r"))==0) {
		cout << "Cannot open file " << filename << endl;
		return false;
	}
	char buffer[1024];
	int mailerBuffer = atoi(fgets (buffer , 1024 , file));
	N = atoi(fgets (buffer , 1024 , file)) + 1 ; //////////////////shell is 0
	mailer = new Mailer(mailerBuffer);
	do {
		fgets (buffer , 1024 , file);
		vector<int>* neighbors=new vector<int>;
		char* tok = strtok(buffer," ");
		int node = atoi(tok);
		tok = strtok (NULL, " ");
		while (tok != NULL) {
			neighbors->push_back (atoi(tok));
			tok = strtok (NULL, " ");
		}
		mailer->addNode(node, neighbors);
	} while (!feof(file));
	cout << "createNet end"<<endl;
	return true;
}

int main(int argc, char** argv) {
	bool didNetCreated = false;
	bool didStart = false;
	cout << "poop" << endl;
	if (pthread_mutex_init(&msgCount_lock, NULL) != 0)
	{
		perror("Message lock initialization");
		exit(1);
	}
	char str[1024];
	while (true) {
		cout << ">";
		scanf ("%s",str);
		for(char* temp=str; *temp!='\0'; temp++) {
			if (*temp >= 'A' && *temp <= 'Z')
				*temp += 'a' - 'A';
		}
		//cout << "command "<< str <<endl;
		if (strcmp(str,"cn")==0 || strcmp(str,"createnet") == 0) {//hack
			scanf ("%s",str);
			if(didNetCreated=createNet(str))
				cout << "net created." << endl;
			else
				cout << "can't create net." << endl;
		}
		else if (didNetCreated && strcmp(str,"run") == 0) {
			int alg = 0;
			int arg = 0;
			scanf ("%d%d",&alg,&arg);
			didStart = mailer->start(alg,arg);
		}
		else if (strcmp(str,"exit") == 0) {
			break;
		}
		else if (strcmp(str,"help") == 0 || strcmp(str,"menu") == 0|| strcmp(str,"h") == 0|| strcmp(str,"?") == 0) {
						cout << "CreateNet net" <<endl;
						cout << "Run i j" <<endl;
						cout << "Kill i" <<endl;
						cout << "Revive i" <<endl;
						cout << "KillAll" <<endl;
						cout << "PrintRT i" <<endl;
						cout << "SendPacket i j string" <<endl;
						cout << "status"<<endl;
						cout << "counters"<<endl;
						cout << "exit"<<endl;
		}
		else if (didStart) {
			if (strcmp(str,"kill") == 0 || strcmp(str,"killnode") == 0) {
				int node = 0;
				scanf ("%d",&node);
				mailer->killNode(node);
			}
			else if (strcmp(str,"revive") == 0 || strcmp(str,"revivenode") == 0) {
				int node = 0;
				scanf ("%d",&node);
				mailer->reviveNode(node);
			}
			else if (strcmp(str,"killall") == 0) {
				mailer->killAll();
			}
			else if (strcmp(str,"printrt") == 0) {
				int node = 0;
				scanf ("%d",&node);
				mailer->printRT(node);
			}
			else if (strcmp(str,"status") == 0) {
				for(int i=1;i<N;i++)
					mailer->printRT(i);
			}
			else if (strcmp(str,"send") == 0 || strcmp(str,"sendpacket") == 0) {
				int src = 0;
				int target = 0;
				scanf ("%d %d",&src,&target);
				gets(str);
				mailer->sendPacket(src,target,str);
			}
			else if (strcmp(str,"counters") == 0) {
				mailer->printCounters();
			}
		}
		else {
			cout<<"command unknown :("<<endl;
			//syso error? (cant send command before run ends)
		}
	}
	delete mailer;
	cout << "shell out." <<endl;
	return 0;
}
